//
//  HMBaseTableViewController.m
//  HowToDo
//
//  Created by Stan on 2017-01-05.
//  Copyright © 2017 KY. All rights reserved.
//

#import "HMBaseTableViewController.h"
#import "HMMusicImageView.h"
#import "HMShoppingCartImageView.h"
#import "HMSearchTableViewController.h"
#import "HMAudioPlayerController.h"
#import "HMShoppingCarTool.h"
#import "HMShoppingCarTableViewController.h"
@interface HMBaseTableViewController ()
@property(nonatomic, strong) UIView *tipView;

//  音乐gif图片
@property (nonatomic, strong) HMMusicImageView *musicImageView;

//导航栏购物车图片
@property(strong,nonatomic)HMShoppingCartImageView *shoppingCartImageView;
@end

@implementation HMBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //  无数据不显示分割线
    self.tableView.tableFooterView = [[UIView alloc] init];

    [self setObserver];
}

- (UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:self.view.bounds];
    }
    return _tipView;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self updateMusicImageView];
}

- (void)setObserver
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(updateMusicImageView) name:@"AUDIO_PLAYER_NOTIFICATION" object:nil];
}

- (void)updateMusicImageView
{
    if([HMAudioPlayerController sharePlayer].isPlaying)
    {
        [self startAnimation];
    }
    else
    {
        [self stopAnimation];
    }
}
//  无数据提示视图
- (void)showTipViewWithName: (NSString *)imageName andMessage: (NSString *)msg {
    
    //  先删除之前的提示视图，在添加子视图
    [self removeTipView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"empty_placeholder_90x90_"]];
    
    UILabel *msgLabel = [[UILabel alloc] init];
    msgLabel.text = msg;
    msgLabel.font = [UIFont systemFontOfSize:18];
    [msgLabel sizeToFit];
    
    //  添加到视图中
    [self.view addSubview:self.tipView];
    [self.tipView addSubview:imageView];
    [self.tipView addSubview:msgLabel];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tipView).offset(100);
        make.centerX.equalTo(self.tipView);
    }];
    
    [msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(10);
        make.centerX.equalTo(imageView);
    }];
    

}

//  删除提示视图
- (void)removeTipView {
    [self.tipView removeFromSuperview];
}


- (HMShoppingCartImageView *)shoppingCartImageView{
    if (!_shoppingCartImageView) {
        _shoppingCartImageView = [HMShoppingCartImageView shoppingCartImageView];
        _shoppingCartImageView.frame =CGRectMake(0, 0, 35, 35);
    }
    return _shoppingCartImageView;
}

- (HMMusicImageView *)musicImageView {
    if (!_musicImageView) {
        _musicImageView = [HMMusicImageView musicImageView];
        _musicImageView.frame = CGRectMake(0, 0, 35, 35);
    }
    return _musicImageView;
}

//建立两个右侧item的导航栏
- (void)setupNavRightItemsByTwo {
    
    UIBarButtonItem *musicItem = [[UIBarButtonItem alloc]initWithCustomView:self.musicImageView];
    UIBarButtonItem *shoppingCartItem = [[UIBarButtonItem alloc]initWithCustomView:self.shoppingCartImageView];
    
    self.shoppingCartImageView.badge = [[HMShoppingCarTool sharedShoppingCarTool] getShoppingCarCount];
    
    self.navigationItem.rightBarButtonItems = @[musicItem,shoppingCartItem];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeShoppingCartNumber:) name:@"shoppingCarCount" object:nil];
    
    //    musicItem的tap事件
    UITapGestureRecognizer *musicItemtap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickMusicItem:)];
    [self.musicImageView addGestureRecognizer:musicItemtap];
    
    //    shoppingCart的tap事件
    UITapGestureRecognizer *shoppingCartItemtap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickShoppingCartItem:)];
    [self.shoppingCartImageView addGestureRecognizer:shoppingCartItemtap];
    
}

- (void)changeShoppingCartNumber:(NSNotification *)notic{
    NSArray *shoppingCartArray = notic.userInfo[@"shoopingCartArr"];
    self.shoppingCartImageView.badge = shoppingCartArray.count;
    
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//建立一个右侧item的导航栏
- (void)setupNavRightItemsByOne {
    
    UIBarButtonItem *musicItem = [[UIBarButtonItem alloc]initWithCustomView:self.musicImageView];
    
    self.navigationItem.rightBarButtonItem = musicItem;
    
}

//建立左侧搜索item的导航栏
- (void)setupNavLeftItemsBySearch{
    
    UIImage *leftImage = [UIImage imageNamed:@"navbar_search_blod"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(btnClickToSearchTableViewController:)];
    
}

//导航栏搜索功能
- (void)btnClickToSearchTableViewController:(UIBarButtonItem *)send {
    
    HMSearchTableViewController *searchTVC = [[HMSearchTableViewController alloc]init];
    [self.navigationController pushViewController:searchTVC animated:NO];
}


// 跳转到音乐播放器
- (void)clickMusicItem:(UIBarButtonItem *)sender
{
    if([HMAudioPlayerController sharePlayer].isPlaying)
    {
        [self.navigationController pushViewController:[HMAudioPlayerController sharePlayer] animated:YES];
    }
}


//跳转到购物车
- (void)clickShoppingCartItem:(UIBarButtonItem *)sender
{
    HMShoppingCarTableViewController *shoppingCarTVC = [[HMShoppingCarTableViewController alloc]init];
    [self.navigationController pushViewController:shoppingCarTVC animated:YES];
    
}

- (void)startAnimation {
    [self.musicImageView musicStartAnimation];
}

//  停止音乐gif播放
- (void)stopAnimation {
    [self.musicImageView musicStopAnimation];
}
@end
